<?php 
/**
 * 地址管理PHP
 * ============================================================================
 * * 版权所有六象元一科技有限公司。
*/
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
$action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'default';//获取变量
$smarty->assign('footer', get_footer());

assign_template();//读取通用配置
$smarty->assign('action',     $action);//赋值ACT变量

$smarty->assign('top_title',  $_LANG[$action]);//顶部标题
/* 显示页面的action列表 */
$ui_arr = array('address_list', 'address_add','address_up','address_select','act_edit_address','set_default','set_address','address_delete');

/* 未登录处理 */
if (empty($_SESSION['user_id']))
{
	ecs_header("Location: user.php?act=login&user_id=1\n");
}
else{
	$user_id=$_SESSION['user_id'];
}

 if (!in_array($action, $ui_arr)){
	ecs_header("Location: index.php");
 }
//选择收货地址
if ($action=='address_select') {
    // 判断是否恶意操作
    $rid = !empty($_REQUEST['rid']) ? $_REQUEST['rid'] : 0;
    // 勾选是否有值
    if (empty($rid) || empty($_SESSION[md5($rid)])) {
        ecs_header("Location: flow.php\n");     //跳转到购物车页面
        exit();
    } else {
        $rec_id = $_SESSION[md5($rid)];
        $smarty->assign('rid',              $_REQUEST['rid']);
    }

	include_once(ROOT_PATH . 'mobile/includes/lib_transaction.php');
    /* 实例化订单操作类 */
    require_once(ROOT_PATH . 'includes/classOrder.php');
    $cOrder  = new classOrder;
    $consignee_list = $cOrder->getUserAddrList();    /* 获得用户所有的收货人信息 */
    $count = count($consignee_list);
    if ($consignee_list) {
        foreach ($consignee_list AS $region_id => $consignee)
        {
            $consignee_list[$region_id]['region'] = selectaddress($consignee['province']) . selectaddress($consignee['city']) . selectaddress($consignee['district']);
        }
    }
    $smarty->assign('consignee_list', $consignee_list);
    $smarty->assign('consignee', $cOrder->consignee);
    $back_url = 'flow.php?step=checkout&rid=' . $_REQUEST['rid'];
    //赋值于模板
    $smarty->assign('count',    $count);
    $smarty->assign('back_url',      $back_url);
	$smarty->display('address.dwt');
}

//处理AJAX选择收货地址
if($action=='set_address'){

    /*------------------------------------------------------ */
    //-- 判断是否恶意操作
    /*------------------------------------------------------ */
    $rid = !empty($_REQUEST['rid']) ? $_REQUEST['rid'] : 0;
    // 勾选是否有值
    if (empty($rid) || empty($_SESSION[md5($rid)])) {
        ecs_header("Location: flow.php\n");     //跳转到购物车页面
        exit();
    } else {
        $rec_id = $_SESSION[md5($rid)];
        $smarty->assign('rid',              $_REQUEST['rid']);
    }

    include_once('includes/cls_json.php');
    $json = new JSON;
    $address_id = intval($_REQUEST['address_id']);
    $sql="UPDATE ".$ecs->table('users')." SET address_id = '$address_id'".
    " WHERE user_id=" . $_SESSION['user_id'];
    $db->query($sql);
    // 更新支付方式为支付宝
    $_SESSION['pay_id'] = 1;
    /* 实例化订单操作类 */
    require_once(ROOT_PATH . 'includes/classOrder.php');
    $cOrder  = new classOrder;
    //查询地址信息
    $cOrder->getUserAddrList($address_id);
    $consignee = $cOrder->consignee;
    $area_id = is_buy($consignee)?is_buy($consignee):100;
    $consignee['addr'] = selectaddress($consignee['province']).selectaddress($consignee['city']).selectaddress($consignee['district']);
    if( $consignee ){
        echo $json->encode("flow.php?step=checkout&rid=" . $_REQUEST['rid']);
    }
    else{
        echo $json->encode("address.php?act=address_select&rid=" . $_REQUEST['rid']);
    }

}
//处理AJAX删除收货地址
if($action=='address_delete'){
    include_once('includes/cls_json.php');
    $json = new JSON;
    $address_id = intval($_REQUEST['address_id']);
    $message['delete_id']='#d_'.$address_id;
    $sql="DELETE FROM ".$ecs->table('user_address').
    " WHERE user_id=".$user_id." AND address_id=".$address_id;
    $delete_address=$db->query($sql);
    //查询地址信息
    if($delete_address){
        $message['message']=1;
         echo $json->encode($message);
    }
    else{
        $message['message']='删除失败请刷新页面后重试';
        echo $json->encode($message);
    }
}


//收货地址列表
if($action=='address_list'){
	include_once(ROOT_PATH . 'mobile/includes/lib_transaction.php');
    /* 获得用户所有的收货人信息 */
    $consignee_list = get_consignee_list($_SESSION['user_id']);
    $count=count($consignee);
    foreach ($consignee_list AS $region_id => $consignee)
    {
        $consignee_list [$region_id]['region']    = selectaddress($consignee['province']).selectaddress($consignee['city']).selectaddress($consignee['district']);
    }
    $smarty->assign('consignee_list', $consignee_list);
    /* 获取默认收货ID */
    $address_id  = $db->getOne("SELECT address_id FROM " .$ecs->table('users'). " WHERE user_id='$user_id'");
    //赋值于模板
    $smarty->assign('count',    $count);
    $smarty->assign('address',          $address_id);
	$smarty->display('address.dwt');
}

/*AJAX设定默认地址*/
elseif ($action == 'set_default') {

    include_once('includes/cls_json.php');
    $json = new JSON;
    $address_id = $_POST['address_id'];
    $sql="UPDATE ".$ecs->table('users')." SET address_id='$address_id'".
    " WHERE user_id=".$user_id;
    $up_address=$db->query($sql);
    if($up_address){
        echo $json->encode("修改成功");
    }
    else{
        echo $json->encode("修改错误");
    }
}
/*添加、修改收货地址页面*/
elseif ($action == 'address_add') {

    /*------------------------------------------------------ */
    //-- 判断是否恶意操作
    /*------------------------------------------------------ */
    $rid = !empty($_REQUEST['rid']) ? $_REQUEST['rid'] : 0;
    // 勾选是否有值
    if (empty($rid) || empty($_SESSION[md5($rid)])) {
        ecs_header("Location: flow.php\n");     //跳转到购物车页面
        exit();
    } else {
        $rec_id = $_SESSION[md5($rid)];
        $smarty->assign('rid',              $_REQUEST['rid']);
    }
    $smarty->display('address.dwt');
}
/*修改收货地址页面*/
elseif ($action == 'address_up') {

    /*------------------------------------------------------ */
    //-- 判断是否恶意操作
    /*------------------------------------------------------ */
    $rid = !empty($_REQUEST['rid']) ? $_REQUEST['rid'] : 0;
    // 勾选是否有值
    if (empty($rid) || empty($_SESSION[md5($rid)])) {
        ecs_header("Location: flow.php\n");     //跳转到购物车页面
        exit();
    } else {
        $rec_id = $_SESSION[md5($rid)];
        $smarty->assign('rid',              $_REQUEST['rid']);
    }

    $address_id=$_REQUEST['address_id'];

    if(empty($address_id)){
       ecs_header("Location: index.php");
    }
    else{
        $consignee=get_consignee_list($_SESSION['user_id'],$address_id);
        $consignee['region'] = selectaddress($consignee['province']).selectaddress($consignee['city']).selectaddress($consignee['district']);
        $consignee['province_name']=selectaddress($consignee['province']);
        $consignee['city_name']=selectaddress($consignee['city']);
        $consignee['district_name']=selectaddress($consignee['district']);
    }
    // 默认省市区列表
    $province_list = get_regions(1, 1);
    $city_list     = get_regions(2, $consignee['province']);
    $district_list = get_regions(3, $consignee['city']);
    $smarty->assign('province_list', $province_list);
    $smarty->assign('city_list',     $city_list);
    $smarty->assign('district_list', $district_list);
    $smarty->assign('consignee', $consignee);
    $smarty->assign('daodansong_list', get_daodansong());
    $smarty->assign('keys', $_REQUEST['key']);
    $smarty->display('address.dwt');
}
/* 添加/编辑收货地址的处理 */
elseif ($action == 'act_edit_address')
{
    /*------------------------------------------------------ */
    //-- 判断是否恶意操作
    /*------------------------------------------------------ */
    $rid = !empty($_REQUEST['rid']) ? $_REQUEST['rid'] : 0;
    // 勾选是否有值
    if (empty($rid) || empty($_SESSION[md5($rid)])) {
        ecs_header("Location: flow.php\n");     //跳转到购物车页面
        exit();
    } else {
        $rec_id = $_SESSION[md5($rid)];
        $smarty->assign('rid',              $_REQUEST['rid']);
    }

    include_once(ROOT_PATH . 'mobile/includes/lib_transaction.php');
    include_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/shopping_flow.php');
    $smarty->assign('lang', $_LANG);
    $count =  count(get_consignee_list($_SESSION['user_id']));
    $count = !empty($count)?$count:1;
    $consignee=get_consignee_region(intval($_POST['district']),intval($_POST['daodansong']));
    if(!$consignee) {
        show_message("你输入的地址信息有误");
        exit;
    }
    $address = array(
        'num'        => $count,
        'user_id'    => $user_id,
        'country'    => $consignee['country'],
        'province'   => $consignee['province'],
        'city'       => $consignee['city'],
        'district'   => $consignee['district'],
        'address'    => isset($_POST['address'])   ? compile_str(trim($_POST['address']))    : '',
        'consignee'  => isset($_POST['consignee']) ? compile_str(trim($_POST['consignee']))  : '',
        'mobile'     => isset($_POST['mobile'])    ? compile_str(make_semiangle(trim($_POST['mobile']))) : '',
        'daodansong_id'     => $consignee['daodansong']['area_id']?$consignee['daodansong']['area_id']:'0',
        );
    if(in_array("",$address)){
        show_message("你输入的地址信息有误");
        exit;
    }
    $address['user_id']=$_SESSION['user_id'];
    $address['address_id']=intval($_POST['address_id']);
    if (update_address($address))
    {
        ecs_header("Location: address.php?act=address_select&rid=" . $_REQUEST['rid']);
    }
}
 ?>